function SaleRecordAdd_Code() {
	var buffer = new StringBuffer();
	buffer.append('<div class="crmsheet" style="height:120px; padding-bottom:0px;"><textarea id="txt_context" placeholder="填写销售记录" style="height:100%;"></textarea></div>');
	buffer.append('<div class="addphoto" style="width:'+(swidth-10)+'px;"><div id="img_src"><div id="addphoto" style="float:left"><img class="add" src="img/add-photo.png" style="width:'+(swidth-50)/4+'px;height:'+(swidth-50)/4+'px;" /><div class="imgload"></div></div></div></div>');
	buffer.append('<div id="nextcalltime" class="crmsheet border-top-bottom"><div class="ctitle" style="width:30%"><div class="cname">下次回访时间</div></div><div class="ctext" style="width:60%"><input id="txt_nextcalltime" type="text" placeholder="" value="" readonly="readonly"/></div><div class="carrow"><img src="img/arrow.png"/></div></div>');
	buffer.append('<div class="crmsheet border-bottom"><div class="ctitle" style="width:30%"><div class="cname">回访提醒</div></div><div class="ctext" style="width:60%"><select id="txt_nextremindtime"><option value="-1">不提醒</option><option value="0">按时提醒</option><option value="5">提前5分钟</option><option value="10">提前10分钟</option><option value="15">提前15分钟</option><option value="30">提前30分钟</option><option value="60">提前1小时</option><option value="120">提前2小时</option><option value="360">提前6小时</option><option value="1440">提前1天</option><option value="2880">提前2天</option></select></div><div class="carrow"><img src="img/arrow.png"/></div></div>');
	buffer.append('<div class="crmsheet border-bottom"><div class="ctitle" style="width:30%"><div class="cname">销售动作</div><div class="require">*</div></div><div class="ctext" style="width:60%"><select id="txt_action"></select></div><div class="carrow"><img src="img/arrow.png"/></div></div>');
	return buffer.toString();
};

function SaleRecord_Submit()
{
	upload(function(imgs){
		var data = {};
		data.context = $("#txt_context").val();
	    data.action = $("#txt_action").val();
	    data.nextcalltime = $("#txt_nextcalltime").val();
	    data.nextremindtime = $("#txt_nextremindtime").val();
	    data.customerid = obj.attr("id");
	    if(imgs!=""){
	    	data.imgs = eval(imgs).join(",");
	    }else{
	    	data.imgs="";
	    }
	    data.source = source; //1代表来自PC端
	    data.version = version; //浏览器型号
	    data.act = "Save_SaleRecord";
		data.userid=userid;
		data.comid=comid;
		if(data.context.Trim()=="")
		{
			plus.nativeUI.toast("请填写销售记录");
			return false;
		}
		if(data.action.Trim()=="")
		{
			plus.nativeUI.toast("请选择销售动作");
			return false;
		}
		data.sign=GetSign(data.act);
		mui.post(crm_domain, data, function(json) {
			plus.nativeUI.toast(json.msg);
			if(json.state)
			{
				CloseWindow();
				CustomerInfo_Search();
				FindCrmReport();
			}
		},'json');
	});
};

function Init_SaleRecordAdd()
{
	/*
	get("nextcalltime").addEventListener("tap",function(){
		plus.nativeUI.pickDate( function(e){
				var d=e.date;
				$("#nextcalltime input").val((d.getFullYear()+"-"+(d.getMonth()+1)+"-"+d.getDate()).ToDate());
			},function(e){
				
		});
	});
	*/
	var currYear = (new Date()).getFullYear();	
			var opt={};
			opt.date = {preset : 'date'};
			opt.datetime = {preset : 'datetime'};
			opt.time = {preset : 'time'};
			opt.default = {
				theme: 'android-ics light', //皮肤样式
		        display: 'bottom', //显示方式 
		        mode: 'scroller', //日期选择模式
				dateFormat: 'yyyy-mm-dd',
				lang: 'zh',
				showNow: true,
				nowText: "今天",
				rows: 3,
		        startYear: currYear - 10, //开始年份
		        endYear: currYear + 10 //结束年份
			};
		  	var optDateTime = $.extend(opt['datetime'], opt['default']);
		    $("#txt_nextcalltime").mobiscroll(optDateTime).datetime(optDateTime);
	
	
	get("addphoto").addEventListener('tap', function(event) {
				//get("share_context").blur();
						var btnArray = [{
							title: "拍照"
						}, {
							title: "从相册选择"
						}];
						plus.nativeUI.actionSheet({
							title: "选择照片",
							cancel: "取消",
							buttons: btnArray
						}, function(e) {
							var index = e.index;
							switch (index) {
								case 0:
									break; 
								case 1:
									var cmr = plus.camera.getCamera();
									cmr.captureImage(function(path) {
										at_count=0;
										ResizeImg2("file://" + plus.io.convertLocalFileSystemURL(path),1);
										Init_ImgPreview();
									}, function(err) {}); 
									break;
								case 2:
									plus.gallery.pick(function(e) {
										//get("img_panel").style.display="";
										at_count=0;
										for(var i in e.files){
											ResizeImg2(e.files[i],e.files.length);
											Init_ImgPreview();
								    	}
									}, function(err) {}, {filter:"image",multiple:true,maximum:9});
									break;
							}
						}); 
					}, false);
	
	var data = {};
	data.act = "SaleRecord_Tag";
	data.userid=userid;
	data.comid=comid;
	data.sign=GetSign(data.act);
	mui.getJSON(crm_domain, data, function(json) {
		var str="";
		$.each(json.data,function(i,info){
			str+='<option value="'+info.Name+'">'+info.Name+'</option>';
		});
		$("#txt_action").html(str);
	});
};


function ResizeImg2(url,count){
	var image = new Image();
	image.src=url;
	var orientation=null;
	var rotate=0;
	image.onload=function(){ //图片加载完成
    //获取原始照片方向信息
    EXIF.getData(image,function () {
    	var height=1000*image.height/image.width;
        orientation = EXIF.getTag(this, "Orientation");
        if(orientation==1){//0°
            rotate=0;
            height=1000*image.height/image.width;
        }else if(orientation==6){//顺时针90°
            rotate=90;
            height=1000*image.width/image.height;
        }else if(orientation==8){//逆时针90°/顺时针270°
            rotate=270;
            height=1000*image.width/image.height;
        }else if(orientation==3){//180°
            rotate=180;
            height=1000*image.height/image.width;
        }
		var img=url.substring(url.lastIndexOf("/")+1,url.length).split(".")[0];
		plus.zip.compressImage(
					    {
					      src: url,
					      dst: '_doc/'+img+'_o.jpg',
					      overwrite: true,
					      width: '1000px', //这里指定了宽度，同样可以修改
					      height: parseInt(height)+'px',
					      format: 'jpg',
					      quality: 50  //图片质量不再修改，以免失真 
					    },
					    function(e) {
					      
					      $("<img class='up_img' src='" + e.target +"' width='"+(swidth-50)/4+"' height='"+(swidth-50)/4+"'/>").insertBefore("#addphoto");
					      at_count++;
                            $(".imgload").html("加载"+parseInt(parseFloat(at_count)/count*100)+"%");
                            if(at_count==count){
                            	$(".imgload").hide();
                            }
					    },
					    function(err) {
					      plus.nativeUI.alert('未知错误！',function() {
					        mui.back();
					      });
					    }
		);
	});
    }
};
